from datetime import datetime

from django.db import models

from DjangoUeditor.models import UEditorField

# Create your models here.


class GoodsCategory(models.Model):
    """
    商品分类
    """
    CATEGORY_TYPE = (
        (1, "一级类目"),
        (2, "二级类目"),
        (3, "三级类目"),
    )

    name = models.CharField(default="", max_length=30, verbose_name="类别名", help_text="类别名")
    code = models.CharField(default="", max_length=30, verbose_name="类别code", help_text="类别code")
    desc = models.TextField(default="", verbose_name="类别描述", help_text="类别描述")
    category_type = models.CharField(choices=CATEGORY_TYPE, verbose_name="类目级别", help_text="类目级别", max_length=30)
    parent_category = models.ForeignKey("self", null=True, blank=True, verbose_name="父类目级别", help_text="父目录", related_name="seb_cat", on_delete=models.DO_NOTHING)
    is_tab = models.BooleanField(default=False, verbose_name="是否导航", help_text="是否导航")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")  # 这里不能加 ()， 如果加了的话，就会用编译的时间作为当前时间

    class Meta:
        verbose_name = "商品分类"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsCategoryBrand(models.Model):
    """
    商品品牌
    """
    name = models.CharField(default="", max_length=30, verbose_name="品牌名", help_text="品牌名")
    category = models.ForeignKey(GoodsCategory, null=True, blank=True, verbose_name="商品分类", on_delete=models.DO_NOTHING)
    desc = models.CharField(default="", max_length=30, verbose_name="品牌描述", help_text="品牌描述")
    image = models.ImageField(max_length=200, upload_to="brand/images/")

    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")  # 这里不能加 ()， 如果加了的话，就会用编译的时间作为当前时间

    class Meta:
        verbose_name = "商品品牌"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(models.Model):
    """
    商品
    """
    name = models.CharField(default="", max_length=300, verbose_name="商品名称")
    category = models.ForeignKey(GoodsCategory, null=True, blank=True, verbose_name="商品分类", on_delete=models.DO_NOTHING)
    goods_sn = models.CharField(max_length=50, default="", verbose_name="商品统一货号")
    sold_num = models.IntegerField(default=0, verbose_name="商品销量")
    fav_num = models.IntegerField(default=0, verbose_name="收藏数")
    goods_num = models.IntegerField(default=0, verbose_name="库存数")
    market_price = models.FloatField(default=0, verbose_name="市场价格")
    shop_price = models.FloatField(default=0, verbose_name="本店价格")
    goods_brief = models.TextField(max_length=500, verbose_name="商品简短描述")

    goods_desc = UEditorField(verbose_name="内容", imagePath="goods/images/", width=1000, height=300, filePath="goods/files/", default="")

    goods_image = models.TextField(default="", verbose_name="商品图片", help_text="商品图片")  # 商品图片 调用阿里云的OSS 跟教程的不一样
    goods_front_image = models.TextField(default="", verbose_name="商品封面图片", help_text="商品封面图片")  # 商品封面图片 调用阿里云的OSS 跟教程的不一样

    ship_free = models.BooleanField(default=True, verbose_name="是否承担运费")  # 邮费
    is_new = models.BooleanField(default=False, verbose_name="是否新品")
    is_hot = models.BooleanField(default=False, verbose_name="是否热卖")
    click_num = models.IntegerField(default=0, verbose_name="点击数")

    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")  # 这里不能加 ()， 如果加了的话，就会用编译的时间作为当前时间

    class Meta:
        verbose_name = "商品"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Banner(models.Model):
    """
    轮播商品
    """
    goods = models.ForeignKey(Goods, verbose_name="商品", on_delete=models.DO_NOTHING)
    image = models.TextField(default="", verbose_name="轮播图片", help_text="轮播图片")  # 商品图片 调用阿里云的OSS 跟教程的不一样
    index = models.IntegerField(default=0, verbose_name="图片权重")

    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")  # 这里不能加 ()， 如果加了的话，就会用编译的时间作为当前时间

    class Meta:
        verbose_name = "轮播商品"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name
